package insight.miescor.opp.domain;

import org.sormula.Database;
import org.sormula.SormulaException;
import org.sormula.Table;
import org.sormula.annotation.Where;
import org.sormula.annotation.WhereField;
import org.sormula.annotation.Wheres;

@Wheres({
    @Where(name="stage", whereFields=@WhereField(name="stage", comparisonOperator="=")) 
})


public class Workflow {
	
	private int id;
	private int stage;
	private String stagename;
	private int reject;
	private String assignment;
	
	public String getAssignment() {
		return assignment;
	}
	public void setAssignment(String assignment) {
		this.assignment = assignment;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getStage() {
		return stage;
	}
	public void setStage(int stage) {
		this.stage = stage;
	}
	public String getStagename() {
		return stagename;
	}
	public void setStagename(String stagename) {
		this.stagename = stagename;
	}
	public int getReject() {
		return reject;
	}
	public void setReject(int reject) {
		this.reject = reject;
	}
	public static Workflow getWorkflowByStage(Database database, int stage)throws SormulaException{
		Table<Workflow> workflowTable = database.getTable(Workflow.class);
		Workflow workflow = workflowTable.select(stage);
		return workflow;
	}
	
	public static boolean isLastStage(Database database, int stage)throws SormulaException{
		Table<Workflow> workflowTable = database.getTable(Workflow.class);
		Workflow workflow = workflowTable.select(stage+1);
		if(workflow != null)
			return false;
		else 
			return true;
	}
	
	public static boolean isFirstStage(Database database,int stage)throws SormulaException{
		Table<Workflow> workflowTable = database.getTable(Workflow.class);
		Workflow workflow = workflowTable.select(stage);
		if(workflow.getStage() == 1)
			return true;
		else 
			return false;
	}
	
	
	public static String getStageName(Database database, int stage)throws SormulaException{
		Table<Workflow> workflowTable = database.getTable(Workflow.class);
		Workflow workflow = workflowTable.selectWhere("stage", stage);
		if(workflow != null){
			System.out.println(workflow.toString());
			return workflow.getStagename();
		}
		else
			return "Complete";
	}
	@Override
	public String toString() {
		return "Workflow [id=" + id + ", stage=" + stage + ", stagename="
				+ stagename + ", reject=" + reject + ", assignment="
				+ assignment + "]";
	}
	
}
